/*
 * Copyright (c) JoinQuant Development Team
 * Author: Huayong Kuang <kuanghuayong@joinquant.com>
 *
 */

#include <iostream>
#include <string>
#include <cstring>
#include <json/value.h>
#include <json/json.h>

using namespace std;


void readStrJson()
{
	//字符串
  const char* str =
      "{\"praenomen\":\"Gaius\",\"nomen\":\"Julius\",\"cognomen\":\"Caezar\","
      "\"born\":-100,\"died\":-44}";

  /*
  //json内容如下：
  {
		"praenomen":"Gaius",
		"nomen":"Julius",
		"cognomen":"Caezar",
		"born":-100,
		"died":-44
	}
  */

  Json::Reader reader;
  Json::Value root;
//从字符串中读取数据
  if(reader.parse(str,root))
  {
      string praenomen = root["praenomen"].asString();
      string nomen = root["nomen"].asString();
      string cognomen = root["cognomen"].asString();
      int born = root["born"].asInt();
      int died = root["died"].asInt();
      string test = root["test"].asString();  // 没有时返回空字符串 string("")
      int test2 = root["test2"].asInt();  // 没有时返回 0

      cout << praenomen + " " + nomen + " " + cognomen
          << " was born in year " << born
          << ", died in year " << died << endl;
      cout << "test: '" << test << "'" << endl;
      cout << "test2: '" << test2 << "'" << endl;
  }

  //for (int i = 0; i < 10; i++) {
    //Json::Value root;
    //root["test"] = "hello world";
  //}

}


typedef struct {
    char name[16];
    int age;
} MyT;


void testValue() {
    Json::Value jv;

    jv["test"] = "hello world";
    jv["arr"].append(1);

    cout << jv.toStyledString() << endl;

    MyT person;
    strcpy(person.name, "Huoty");
    person.age = 30;
    //jv["person"] = person;
}



int main(void)
{
    readStrJson();
    testValue();

    return 0;
}

